package com.wyq.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.wyq.bean.AdminUser;
import com.wyq.service.AdminUserService;

public class AdminLoginAction extends ActionSupport implements Preparable {
	private AdminUserService adminUserService;
	private AdminUser adminUser;
	
	public AdminUser getAdminUser() {
		return adminUser;
	}

	public void setAdminUser(AdminUser adminUser) {
		this.adminUser = adminUser;
	}

	public void setAdminUserService(AdminUserService adminUserService) {
		this.adminUserService = adminUserService;
	}
	@Override
	public void validate(){
		// TODO Auto-generated method stub
		super.validate();
		try {
			List userList = adminUserService.findAdminUserByPwd(adminUser);
			if(userList.size() <= 0){
				this.addFieldError("usernameerror", "用户名不存在");
			}else{//用户名不可重复
				String password = ((Map)userList.get(0)).get("password").toString();
				if((adminUser.getPassword()).equals(password)){
					
					//将展会的信息保存在session中
					Map session = ActionContext.getContext().getSession();
					session.put("adminUser", (Map)userList.get(0));
					session.put("adminUserList",userList);
				}else {
					this.addFieldError("passworderror", "用户名或密码错误");
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	public String execute() throws Exception{
		//用户登录
		Map session = ActionContext.getContext().getSession();
		Map adminUser = (Map) session.get("adminUser");
		if(adminUser == null){
			return INPUT;
		}
			return SUCCESS;
			
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		clearFieldErrors();
	}

}
